package cn.dt70.controller;

import cn.dt70.pojo.AqbInterest;
import cn.dt70.pojo.AqbPacket;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbInterestandcodeService;
import cn.dt70.service.AqbPacketService;
import cn.dt70.utils.CompareDate;
import cn.dt70.utils.Constants;
import cn.dt70.utils.DateUtils;
import cn.dt70.utils.PageSupport;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/hongbao/")
public class HongbaoController {

    private static final Logger logger = Logger.getLogger(HongbaoController.class);

    @Resource
    private AqbPacketService aqbPacketService;

    @Resource
    private AqbInterestandcodeService aqbInterestandcodeService;
    //显示个人账户页面
    @RequestMapping("show")
    public String showPersonCount(){
        return "usercenter-rewardcontrol-redpacket";
    }

    //显示红包的展示页面
    //@RequestParam(value = "queryname",required=false)String queryname,
    @RequestMapping("hongbaoList")
    @ResponseBody
    public String showHongbaoList(@Param("status") String status, PageSupport ps, HttpSession session,
                                  @Param("currentPageNo") String currentPageNo){

        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        if (null == status || "".equals(status)) {
            status = "0";
        }
        //根据红包天数更新红包状态（是否到期）
        if (Integer.parseInt(status) == 1) {
            changeStatus(aqbUser, status);
        }
        if (null == currentPageNo || "".equals(currentPageNo)) {
            ps.setCurrentPageNo(0);
        } else {
            ps.setCurrentPageNo(Integer.parseInt(currentPageNo));
        }

        List<AqbPacket> aqbPacketList = aqbPacketService.getAllAqbPacketByStatus(ps,aqbUser.getUserId(), Integer.valueOf(status) - 1);
        int count=aqbPacketService.sumPacket(aqbUser.getUserId());
        String aqbaqbPacket = JSON.toJSONString(aqbPacketList);
        String phs = JSON.toJSONString(ps);
        String hongbaoCount=JSON.toJSONString(count);
        StringBuffer sb = new StringBuffer("{");
        sb.append("\"aqbaqbPacket\":" + aqbaqbPacket + "");
        sb.append(",\"phs\":" + phs + "");
        sb.append(",\"status\":" + status + "");
        sb.append(",\"hongbaoCount\":" + hongbaoCount + "");
        sb.append("}");
        return sb.toString();
    }

//    红包使用的页面的展示
    @RequestMapping("useHongbao")
    @ResponseBody
    public String showUseHongbao(HttpSession session,
                                 @Param("minMoney")String minMoney){

        if(null == minMoney || "".equals(minMoney)){
            minMoney = "0";
        }
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        //判断红包是否到期
        changeStatus(aqbUser, "1");
        List<AqbPacket> listAqbPacket = aqbPacketService.listHongbao(aqbUser.getUserId(),Integer.valueOf(minMoney));

        StringBuffer sb = new StringBuffer("{");
        sb.append("\"listAqbPacket\":" + JSON.toJSONString(listAqbPacket) + "");
        sb.append("}");
        return sb.toString();
    }

    //判断红包是否到期，并改变状态
    public void changeStatus(AqbUser aqbUser, String status) {
        List<AqbPacket> aqbPacketList = aqbPacketService.listApacket(aqbUser.getUserId(), Integer.valueOf(status) - 1);
        for (AqbPacket aq : aqbPacketList) {
            String date1 = CompareDate.getResultDate(DateUtils.format(aq.getCreateTime()), aq.getHongTimeDuration().intValue());
            String date2 = CompareDate.getResult(0);
            if (CompareDate.compare(date1, date2)) {
                if (aqbPacketService.updateHongbaoStatus(aq.getHoneId(),2)) {
                    System.out.println("用户id为：" + aq.getUserId() + "的红包到期，自动执行到期代码");
                }
            }
        }
    }
}
